﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using MobileWebsiteGenerator.Entities;
using System.Data;
using MobileWebsiteGenerator.Utility;
using Mvc.Mailer;
using MobileWebsiteGenerator.Mailers;
using CaptchaMVC.Attribute;
using CaptchaMVC.HtmlHelpers;
using CaptchaMVC.Models;
using MVCCaptchaDemo.Models;
using MobileWebsiteGenerator;
using MobileWebsiteGenerator.Controllers;



public class CustomerController : HomeController
{
    private IUserMailer _userMailer = new UserMailer();
    public IUserMailer UserMailer
    {
        get { return _userMailer; }
        set { _userMailer = value; }
    }
    public Mobile_appContext context = new Mobile_appContext();
    // GET: /Customer/Create
    public ActionResult Create()
    {
        return View(MobileWebsiteGenerator.MvcApplication._MainCustomer.customer);
    }

    //
    // POST: /Customer/Create

    [HttpPost]
    public ActionResult Create(Customer customer)
    {

        if (ModelState.IsValid)
        {

            MvcApplication.IsUpdateMode = false;
            if (context.Customers.Where(c => c.Email == customer.Email).FirstOrDefault() == null)
            {
             
                MvcApplication._MainCustomer.customer = customer;
                ViewBag.CustomerID = customer.Customer_id;
                MvcApplication.IsLoggedIn = true;
                MvcApplication.showMessage = true;
                MvcApplication.messageTxt = "You Are Successfully Registered.";
                MvcApplication._MainCustomer.customer.IsActive = true;
                context.Customers.Add(MvcApplication._MainCustomer.customer);
               // _userMailer.Welcome(customer).Send();
                context.SaveChanges();
            }
            else
            {
                MvcApplication.showMessage = true;
                MvcApplication.messageTxt = "User Already exist please login to continue.";
                return RedirectToAction("Create", customer);
            }
            return RedirectToActionPermanent("Index", "ThemeDetail");
        }

        return View(customer);
    }

    public CaptchaImageResult ShowCaptchaImage()
    {
        return new CaptchaImageResult();
    }

    //
    // GET: /Customer/Edit/5

 
    public ActionResult Edit(int id)
    {
        Customer customer = context.Customers.Single(x => x.Customer_id == id);

        return View(customer);
    }

    //
    // POST: /Customer/Edit/5

    [HttpPost]
    public ActionResult Edit(Customer customer)
    {
        MvcApplication._MainCustomer.customer.IsActive = true;
        context.Entry(customer).State = EntityState.Modified;
  

        context.SaveChanges();
        return RedirectToAction("UserHome", "Customer");
    }


    public ActionResult UserHome()
    {
        Customer customer = new Customer();
        return View(customer);
    }


    public ActionResult Account(int id)
    {
        Customer customer = context.Customers.Single(x => x.Customer_id == id);
        return View(customer);
       
    }

    [HttpPost]
    public ActionResult Account(Customer customer)
    {
        if (ModelState.IsValid)
        {
            context.Entry(customer).State = EntityState.Modified;
            context.SaveChanges();
            return RedirectToAction("");
        }
        return View(customer);
    }






}
